package Panels;

import java.awt.BorderLayout;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import DatabaseCommands.DatabaseInfo;


public class TableView extends JPanel{
	
	DatabaseInfo DInfo=DatabaseInfo.getInstance();
	private ResultSet Results;
	
	public TableView(){
		setLayout(new BorderLayout());
		try{
			Results=DInfo.GetConnection().createStatement().executeQuery("Select * From "+DInfo.CurrentTable.GetTableName());
		}catch(Exception e){
			System.out.println("Query Error");
		}
		
		
		List<String> Columns=DInfo.CurrentTable.GetColumns();
		ArrayList<ArrayList<String>> Keys=DInfo.CurrentTable.GetKeys();
		
		DefaultTableModel TModel=new DefaultTableModel(Columns.toArray(),0);
		JTable Table=new JTable(TModel);
			
		
		Vector<String> Row=new Vector<String>(Columns.size());
		TModel.addRow(new Vector<>(Row));
		
		try{
			while(Results.next()){
				for(int x=1;x<=Columns.size();x++){
					Row.add(Results.getString(x));
				}
				TModel.addRow(new Vector<>(Row));
				Row.clear();
			}
		}catch(Exception e){
			System.out.println("Results Error");
		}
		
		JScrollPane ScrollPane=new JScrollPane();
		ScrollPane.setViewportView(Table);
		add(ScrollPane,BorderLayout.CENTER);
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
